package com.enfo.zjabank.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhoum on 2016/7/27.
 */
@Repository
public class PrintDaoImpl extends BaseDao implements PrintDao {

    public Map<String, Object> getCompanyApply(String debt_id,String busi_type){
        String primaryKey = "new".equals(busi_type)?"debt_id":"modify_id";
        String tableName = "new".equals(busi_type)?"buz_escape_debts":"buz_escape_modify";
        String sql = "SELECT desc1,desc2,desc3,linkman,provd_grt, input_dept, input_user, input_tel, input_phone, escaper_name, organization_code,getAddress(address) address, legal_person,legal_person_id,legal_rperson,legal_rperson_id, stockholder,relv_person,grt_info, possibility, getYesOrNo(police_state) police_state, getYesOrNo(police_rState) police_rState, police_no, getYesOrNo(justice_state) justice_state, justice_no, getYesOrNo(justice_exeState) justice_exeState, justice_exeNo, getYesOrNo(bnkruptcy_state) bnkruptcy_state, difficult, getYesOrNo(bc_report) bc_report, suggestion,debt_fact, getparamname('sanction_mode',sanction_mode) sanction_mode, stockholder, relv_person, debt_info FROM "+tableName+" WHERE escaper_type=2 AND report_state IN ('1020','1050', '1040') AND "+primaryKey+"=?";
        Map<String,Object> map = queryForMap(sql, new String[]{debt_id});
        return map;
    }

    @Override
    public Map<String, Object> getPersonApply(String debt_id,String busi_type) {
        String primaryKey = "new".equals(busi_type)?"debt_id":"modify_id";
        String tableName = "new".equals(busi_type)?"buz_escape_debts":"buz_escape_modify";
        String sql = "SELECT desc1,desc2,desc3,linkman,provd_grt,input_dept, input_user, input_tel, input_phone, escaper_name, id_card,getAddress(address) address, marriage, getAddress(live_status) live_status, work_unit, possibility, getYesOrNo(police_state) police_state, getYesOrNo(police_rState) police_rState, police_no, getYesOrNo(justice_state) justice_state, justice_no, getYesOrNo(justice_exeState) justice_exeState, justice_exeNo, getYesOrNo(bnkruptcy_state) bnkruptcy_state, difficult, getYesOrNo(bc_report) bc_report, suggestion,debt_fact, getparamname('sanction_mode',sanction_mode) sanction_mode,stockholder,relv_person,grt_info,debt_info FROM "+tableName+" WHERE escaper_type=1 AND report_state IN ('1020','1050', '1040') AND "+primaryKey+"=?";
        return queryForMap(sql, new String[]{debt_id});
    }

    @Override
    public Map<String, Object> getCompanyCancel(String debt_id) {
        String sql = "SELECT linkman,input_dept, input_user, input_tel,input_phone, escaper_name, organization_code, legal_person, legal_person_id, legal_rperson, legal_rperson_id, stockholder, relv_person, back_money, back_interest, cancel_reason FROM buz_escape_debts WHERE escaper_type=2 AND cancel_state='1010' AND debt_id=?";
        return queryForMap(sql, new String[]{debt_id});
    }

    @Override
    public Map<String, Object> getPersonCancel(String debt_id) {
        String sql = " SELECT linkman,input_dept, input_user, input_tel,input_phone, escaper_name, id_card, getAddress(address) address, marriage, getAddress(live_status) live_status, work_unit, back_money,back_interest,cancel_reason FROM buz_escape_debts WHERE escaper_type=1 AND cancel_state='1010' AND debt_id=?";
        return queryForMap(sql, new String[]{debt_id});
    }

    @Override
    public Map<String, Object> getCompanyWarning(String id) {
        String sql = "select sign_date,organization_code,escaper_name,id_card,notice_date,contract_name,contract_no,principal_sum,interest_sum,under_principal,debit_interest,total_sum,over_days,noticeend_date,notice_user,notice_tel,CONCAT(CAST(YEAR(input_time) as CHAR),'年',CAST(MONTH(input_time) as CHAR),'月',CAST(DAY(input_time) as CHAR),'日') input_time from buz_escape_debts where escaper_type=2 and debt_id=?";
        return queryForMap(sql, new String[]{id});
    }

    @Override
    public Map<String, Object> getPersonWarning(String id) {
        String sql = "select sign_date,id_card organization_code,escaper_name,id_card,notice_date,contract_name,contract_no,principal_sum,interest_sum,under_principal,debit_interest,total_sum,over_days,noticeend_date,notice_user,notice_tel,CONCAT(CAST(YEAR(input_time) as CHAR),'年',CAST(MONTH(input_time) as CHAR),'月',CAST(DAY(input_time) as CHAR),'日') input_time from buz_escape_debts where escaper_type=1 and debt_id=?";
        return queryForMap(sql, new String[]{id});
    }

    /**
     * 当report_date状态为1010的时候打印警示函同时更新report_date状态为1020，并且更新noticeend_date为notice_date+30天，日期格式为YYYY-MM-DD
     * @param id
     */
    @Override
    public void changeState(String id) {
        String sql = "update buz_escape_debts set report_state=1020,noticeend_date=DATE_FORMAT(date_add(notice_date,interval 30 day),'%Y%m%d') where report_state=1010 and debt_id=?";
        this.jdbcTemplate.update(sql, new Object[]{id});
    }

    @Override
    public Map<String, String> getDict(String paramKey) {
        String sql = "select param_value,param_name from org_tparam where param_key = ?";
        final Map<String,String> dictMap = new HashMap<>();
        this.jdbcTemplate.query(sql,new Object[]{paramKey}, new RowCallbackHandler(){
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                dictMap.put(rs.getString("param_value"),rs.getString("param_name"));
            }
        });
        return dictMap;
    }
}
